package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanLevelQO;
import com.xingkeduo.pojo.table.CraftsmanLevelTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;

/**
 * 一线员工职级封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:26:24
 * @description
 */
@Component
public class CraftsmanLevelJTO extends BaseJson {
    public static CraftsmanLevelTO createTableForNew(String json) throws Throwable {
        CraftsmanLevelTO table = new CraftsmanLevelTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setSequence(validSequence(root, YesNoEnum.NO)).setName(validName(root, YesNoEnum.YES)).setIsDelete(validIsDelete(root, YesNoEnum.NO));

        return table;
    }

    public static CraftsmanLevelTO createTableForModify(String json) throws Throwable {
        CraftsmanLevelTO table = new CraftsmanLevelTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setSequence(validSequence(root, YesNoEnum.NO)).setName(validName(root, YesNoEnum.NO))
                .setIsDelete(validIsDelete(root, YesNoEnum.NO));

        return table;
    }

    public static CraftsmanLevelQO createQueryForList(String json) throws Throwable {
        CraftsmanLevelQO query = new CraftsmanLevelQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setCode(validCode(root, YesNoEnum.NO)).setName(validName(root, YesNoEnum.NO)).setIsDelete(validIsDelete(root,YesNoEnum.NO)).setNameOrId(validNameOrId(root, YesNoEnum.NO));
        return query;
    }

    public static CraftsmanLevelQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanLevelQO query = new CraftsmanLevelQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setId(validId(root, YesNoEnum.YES));

        return query;
    }

    public static ObjectNode toJson(CraftsmanLevelTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            if (table.getName() != null)
                root.put("name", table.getName());
            if (table.getSequence() > 0)
                root.put("sequence", table.getSequence());
            if (table.getIsDelete() != null) {
                root.put("isdelete_value", table.getIsDelete().getValue());
                root.put("isdelete_desc", table.getIsDelete().getDesc());
            }

            toBaseJson(root, table);
        }

        return root;
    }

    public static ObjectNode toJsonCodeAndName(CraftsmanLevelTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {
            if (table.getName() != null)
                root.put("name", table.getName());
            if (table.getSequence() > 0)
                root.put("sequence", table.getSequence());

            toBaseJson(root, table);
        }

        return root;
    }



    public static ArrayNode toJson(List<CraftsmanLevelTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<CraftsmanLevelTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }

    public static int validSequence(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "sequence";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Integer.parseInt(value);
        }
        return 0;
    }

    /**
     * 查询条件 name和id
     *
     * @param node:
     * @param isRequired:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/13 19:26
     */
    public static String validNameOrId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "nameOrId";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value))
                return null;

            return value;
        }
        return null;
    }

    public static String validName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "name";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 20)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static YesNoEnum validIsDelete(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "isdelete";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }
}
